package com.SmartWallet;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.*;
import android.graphics.*;
import android.widget.TextView;
import android.widget.Toast;

import java.util.*;


import com.SmartWallet.models.BudgetList;
import com.SmartWallet.models.ListEntry;

public class ListViewActivity2 extends Activity{
	static HashMap<String,ListEntry> entries;
	static ArrayList<ListEntry> entryList;
	ArrayList<TableRow> rowList;
	static BudgetList list;
	static ArrayList<String> entryNames;
	View v;
	TableLayout tl;
    /** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  entries = new HashMap<String,ListEntry>();
	
	  rowList = new ArrayList<TableRow>();
		
		
		list = 	SmartWalletActivity.sanDiego;
		
	  
	  
	  
	  entryNames = new ArrayList<String>();
	 entryList =  list.getEntries();
	 for(int i = 0; i < entryList.size(); i++)
	 {
		 entryNames.add(entryList.get(i).getName() + " " + entryList.get(i).getType() + " $" + entryList.get(i).getCost());
		 entries.put(entryList.get(i).getName(), entryList.get(i));
	 }
	 v = new View(this);
		v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
		v.setBackgroundColor(Color.rgb(51, 51, 51));
		tl = new TableLayout(this);
	  updateView();
	  tl.addView(v);
		setContentView(tl);
	  

	  
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.entry_menu, menu);
	    return true;
	}
	
	/*public void updateView()
	{
		setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, entryNames.toArray(new String[entryNames.size()])));
		//String[] s = new String[1];
		//s[0]= entryNames.get(0);
		//setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, s));

		  ListView lv = getListView();
		  lv.setTextFilterEnabled(true);
		  lv.setOnItemClickListener(new OnItemClickListener() {
			    

				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
						long arg3) {
					// TODO Auto-generated method stub
					 Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(),
					          Toast.LENGTH_SHORT).show();
					
				}
			  });
	}*/
	
	public void updateView()
	{
		
		
		
		/*tr.setOnClickListener(new  OnClickListener() {

	        
	        public void onClick(View v) {
	           // v.setBackgroundColor(Color.GRAY);
	            // TODO Auto-generated method stub
	            Toast.makeText(getApplicationContext(), entryList.get(0).getComment(),
				          Toast.LENGTH_LONG).show();

	        }
	    });*/
		for(int i = 0; i < entryNames.size(); i++)
		{
			rowList.add(new TableRow(this));
			
			
			
		}
		for(int i = 0; i < entryNames.size(); i++)
		{
			TextView tv = new TextView(this);
			tv.setText(entryNames.get(i));
			rowList.get(i).addView(tv);
			
			
		}
		for(int i = 0; i < entryNames.size(); i++)
		{
			tl.addView(rowList.get(i));
			
			
			
		}
		
		
		
		/*TableRow tr2= new TableRow(this);
		
		
		TextView tv2 = new TextView(this);
		tv2.setText(entryNames.get(0));
		tr.addView(tv); 
		tr2.addView(tv2);*/
		
		//tl.addView(tr2);
		
		
	}
	public void updateList()
	{
		list = 	SmartWalletActivity.sanDiego;
		
		  
		  
		  
		  entryNames = new ArrayList<String>();
		 entryList =  list.getEntries();
		 for(int i = 0; i < entryList.size(); i++)
		 {
			 entryNames.add(entryList.get(i).getName() + " " + entryList.get(i).getType() + " $" + entryList.get(i).getCost());
			 entries.put(entryList.get(i).getName(), entryList.get(i));
		 }
		 
	}
	
	
	@Override
    protected void onStart() {
        super.onStart();
        // The activity is about to become visible.
       
        
    }
    @Override
    protected void onResume() {
        super.onResume();
        // The activity has become visible (it is now "resumed").
        //updateView();
    }
    @Override
    protected void onPause() {
        super.onPause();
        // Another activity is taking focus (this activity is about to be "paused").
    }
    @Override
    protected void onStop() {
        super.onStop();
        // The activity is no longer visible (it is now "stopped")
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // The activity is about to be destroyed.
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.new_entry:
//Intent intent = new Intent(NewEntryActivity.this,ListViewActivity2.class);			
				

				

				//Start next activity
				//ListViewActivity.this.startActivity(intent); 

				//Start next activity
				
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

